package com.tensquare.notice.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.entity.Result;
import com.tensquare.notice.pojo.Notice;
import com.tensquare.notice.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author BAO
 * @version V1.0
 * @Package tensquare.notice.controller
 * @date 2020/12/4 22:02
 * @description:
 */
@RestController
@CrossOrigin
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") String id){
        Notice notice = noticeService.findById(id);
        return Result.success("success",notice);
    }

    /**
     * 根据条件查询并分页
     * @param notice
     * @param current
     * @param size
     * @return
     */
    @PostMapping("search/{current}/{size}")
    public Result findByCondition(@RequestBody Notice notice,
                                  @PathVariable Integer current,
                                  @PathVariable Integer size){
        Page<Notice> page = noticeService.findByCondition(notice, current, size);
        return Result.success("success",page);
    }

    /**
     * 添加保存
     * @param notice
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Notice notice){
        int save = noticeService.save(notice);
        return save>0?Result.success("success",null):Result.fail("fail");
    }

    /**
     * 根据id更新
     * @param notice
     * @return
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Notice notice){
        int i = noticeService.updateById(notice);
        return i>0?Result.success("success",null):Result.fail("fail");
    }

}
